/*
* Copyright (c) 2005, 2017, EVECOM Technology Co.,Ltd. All rights reserved.
* EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.bif.Filter;

import com.bif.dao.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Ted Wang
 * @created 2017/9/18 16:28
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 该方法将在Controller处理之前进行调用，return false则请求中止
     */
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // 获取请求的url
        String url = httpServletRequest.getRequestURI();
        if (StringUtils.equals(url, "/pages/login.jsp")) {
            // 登陆接口，放行
            return true;
        } else {
            HttpSession session = httpServletRequest.getSession();
            String user = (String)session.getAttribute("User");
            if (user != null) {
                return true;
            }
        }
        // 不符合条件的跳转到登录界面
        // 客户端跳转
        httpServletResponse.sendRedirect("/pages/login.jsp");
        return false;
    }

    /**
     * Controller处理之后，ModelAndView返回之前调用这个方法
     */
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    /**
     * 在ModelAndView视图渲染之后调用这个方法，一般此方法用于清理资源
     */
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

